import request from '@/utils/request'



/**
 * @description: 更新全文搜索ES库
 */
export function updateES () {
    return request({
        method: 'POST',
        url: '/mutual-content/es/update',
    })
}


/**
 * @description: 获取内容的某个历史版本
 */
export function getRevisions (id) {
    return request({
        method: 'get',
        url: '/mutual-content/contentHistory/contentID',
        params: {
            contentId: id
        }
    })
}


//获得内容状态列表
export function getContentStatusList (data) {
    return request({
        method: 'get',
        url: '/mutual-content/status/page',
        params: {
            page: data.page,
            contentId: data.contentId,
            size: data.size
        }
    })
}

//获得内容当前状态
export function getContentStatus (id) {
    return request({
        method: 'get',
        url: '/mutual-content/status/contentId',
        params: {
            contentId: id
        }
    })
}

/**
 * @description: 根据状态码和语言获得状态的名字
 * @param {String} typeCode 状态码
 * @param {Array} statusList  状态对象的列表
 * @param {String} lang 当前语言
 * @return {String} 状态在该语言下的名字
 * @author: chen_yt
 */
export function getStatusDescription (typeCode, statusList, lang) {
    for (let i = 0; i < statusList.length; i++) {
        if (statusList[i].code == typeCode) {
            return statusList[i][lang]
        }
    }
}

export function getTagTypeByStatus (status) {
    switch (status) {
        case 'TOP':
            return ''
        case 'PUBLISH':
            return 'success'
        case 'REVOKE':
            return 'info'
        case 'AUDIT':
            return 'warning'
        case 'FAILURE':
            return 'danger'
        case 'DRAFT':
            return 'info'
        default:
            return ''
    }
}

export function getCategoryList (data) {
    return request({
        method: 'get',
        url: '/mutual-content/category/page',
        params: {
            page: data.page,
            size: data.size,
            categoryName: data.name,
            columnName: data.columnName,
            sort: data.sort
        }

    })
}

//根据id获取分类目录详情
export function getCategoryById (id) {
    return request({
        method: 'get',
        url: '/mutual-content/category',
        params: {
            id
        }
    })
}

//获得内容列表
export function getContentList (data) {
    return request({
        method: 'GET',
        url: '/mutual-content/content/page',
        params: {
            page: data.page,
            size: data.size,
            categoryId: data.categoryId,
            createFullname: data.createFullname,
            status: data.status,
            title: data.title,
            startDate: data.startDate,
            endDate: data.endDate,
            date: data.date,
            columnName: data.columnName,
            sort: data.sort
        }
    })
}


//根据id获取内容详情
export function getContentById (id) {
    return request({
        method: 'get',
        url: '/mutual-content/content',
        params: {
            id
        }
    })
}

//根据内容id获取内容的所有详情
export function getContentDetail (data) {
    return request({
        method: 'GET',
        url: '/mutual-content/contentDetail/page',
        params: {
            contentId: data.contentId,
            page: data.page,
            size: data.size
        }
    })
}


/**
 * @description: 获取内容模板
 * @return {Array} 
 */
export function getTemplateList (data) {
    return request({
        method: 'get',
        url: '/mutual-content/template/page',
        params: {
            name: data.name ? data.name : '',
            page: data.page,
            size: data.size,
            columnName: data.columnName,
            sort: data.sort
        }
    })
}

/**
 * @description: 根据Id获取模板的详细信息
 * @param {String} id
 * @return {Object} 
 */
export function getTemplateDetailById (id) {
    return request({
        method: 'get',
        url: '/mutual-content/templateDetail/templateId',
        params: {
            templateId: id
        }
    })
}

/**
 * @description: 根据模板ID获得所有使用该模板的分类目录
 * @param {*} id 模板ID
 */
export function getCategoryByTemplateId (id) {
    return request({
        method: 'get',
        url: '/mutual-content/category/template',
        params: {
            id: id
        }
    })
}




/**
 * @description: 根据可变字段名称filed.name获得该可变字段对象
 * @param {String} filed.name  
 * @param {Array}  filedList 存储了所有可变字段对象的数组
 * @return {Object} filed
 */
export function findFieldByName (name, fieldList) {
    if (fieldList == null) {
        fieldList = []
    }
    for (let i = 0; i < fieldList.length; i++) {
        if (fieldList[i].fieldName == name) {
            return fieldList[i]
        }
    }
    return null
}

/**
 * @description: 为内容添加状态属性
 * @param {Object} 状态对象 
 */
export function addContentStatus (data) {
    return request({
        method: 'POST',
        url: '/mutual-content/status',
        data
    })
}

/**
 * @description: 获取内容最近的发布/置顶时间信息
 * @param {*} 内容ID
 */
export function getLastPublish (id) {
    return request({
        method: 'POST',
        url: '/mutual-content/status/publish',
        params: {
            contentId: id
        }
    })
}

export function updateContentStatus (data) {
    return request({
        method: 'PUT',
        url: '/mutual-content/status',
        data
    })
}
